import commonUtils from '../dist/common/js/common-utils';

/** 
    * 获取时间
    * 20160106173721310
    */
const getJSCurrTime = () => {
  let d, s = "";
  d = new Date();
  s += d.getFullYear();
  let month = d.getMonth() + 1;
  let day = d.getDate();
  let hour = d.getHours();
  let minu = d.getMinutes();
  let sec = d.getSeconds();
  s += month < 10 ? "0" + month : month;
  s += day < 10 ? "0" + day : day;
  s += hour < 10 ? "0" + hour : hour;
  s += minu < 10 ? "0" + minu : minu;
  s += sec < 10 ? "0" + sec : sec;
  s += d.getMilliseconds();
  return s;
}
//验证手机号码
const checkMobile = (mobile) => {
  if (!(/^1\d{10}$/.test(mobile))) {
    return false;
  }
  return true;
}
//生成随机n位数字
const mathRand = (n) => {
  let num = "";
  for (let i = 0; i < n; i++) {
    num += Math.floor(Math.random() * 10);
  }
  return num;
}

// 判断字符串是不是以某个字符串结尾
const endWith = (str, target) => {
  let reg = new RegExp('^.*' + target + '$');
  return reg.test(str)
}

//判断字符串是不是以某个字符串开头
const startWith = (str, target) => {
  let reg = new RegExp("^" + target);
  return reg.test(str);
}
//时间格式化
const moment = (fmt, nt = null) => {
  let date;
  if (nt) {
    date = new Date(nt);
  } else {
    date = new Date();
  }

  const o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3),
    "S": date.getMilliseconds() // 毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (let k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

//获取时间
const getTime = (nowYear, nowMonth, format = null) => {
  nowMonth = Number(nowMonth) - 1;
  let monthStartDate = new Date(nowYear, nowMonth, 1);
  //本月的结束时间
  let monthEndDate = new Date(nowYear, nowMonth + 1, 0);
  let timeStar = Date.parse(String(monthStartDate)); //s
  let timeEnd = Date.parse(String(monthEndDate)); //s
  console.log()
  if (format) {
    return { timeStar: moment(format, timeStar), timeEnd: moment(format, timeEnd) }
  } else {
    return { timeStar, timeEnd }
  }
}
const genUUID = (len = 35, radix = 10) => {
  const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  let uuid = [], i;
  radix = radix || chars.length;
  if (len) {
    for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  } else {
    let r;
    uuid[14] = '4';
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | Math.random() * 16;
        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
      }
    }
  }
  return `p${uuid.join('')}`;
}
module.exports = {
  showLoading: commonUtils.showCustomLoading,
  hideLoading: commonUtils.hideCustomLoading,
  throttle: commonUtils.throttle,
  debounce: commonUtils.debounce,
  getJSCurrTime,
  checkMobile,
  endWith,
  startWith,
  moment,
  getTime,
  genUUID,
  mathRand,
  compareVersion: commonUtils.compareVersion,
  accAdd: commonUtils.accAdd,
  accSub: commonUtils.accSub,
  accDiv: commonUtils.accDiv,
  accMul: commonUtils.accMul,
  formatNumDecimals: commonUtils.formatNumDecimals,
  isObj: commonUtils.isObj,
  nextTick: commonUtils.nextTick,
  isDef: commonUtils.isDef
}
